package com.ruoyi.project.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.common.StaticMethod;
import com.ruoyi.project.service.CloudOssService;
import com.ruoyi.project.service.ShareService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@RestController
@ApiOperation("通用请求处理")
public class WingCommonController {
    private static final Logger log = LoggerFactory.getLogger(WingCommonController.class);

    @Resource
    private ISysConfigService configService;
    @Resource
    private CloudOssService cloudOssService;
    @Resource
    private ShareService shareService;
    /**
     * 通用上传请求
     */
    @PostMapping("/wing-common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            String fileName = cloudOssService.uploadFile(file, false);
            String url = cloudOssService.getQCloudUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            ajax.put("fileVo", StaticMethod.stringToImageFileVoList(url));
            return ajax;
        } catch (Exception e) {
            log.error("", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/wing-common/upload-compress")
    public AjaxResult uploadFileCompress(MultipartFile file, boolean compress) throws Exception {
        try {
            String fileName = cloudOssService.uploadFile(file, compress);
            String url = cloudOssService.getQCloudUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            ajax.put("fileVo", StaticMethod.stringToImageFileVoList(url));
            return ajax;
        } catch (Exception e) {
            log.error("", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     *
     */
    @GetMapping("/wing-common/download/resource")
    public void resourceDownload(String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
        cloudOssService.downloadFile(name, response);
    }


    /**
     * 根据参数键名查询参数值
     */
    @ApiOperation("获取系统配置参数值")
    @GetMapping("/wing-common/getConfigKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        if (StringUtils.equals("sys.user.initPassword", configKey)) {
            return AjaxResult.success();
        }
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }


}
